package com.brogent.contacts;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ContactsActivity extends ListActivity {
    private final String TAG = this.getClass().getSimpleName();
    private CursorAdapter mAdapter;

    static class ViewHolder {
        ImageView icon;
        TextView name;
        TextView phone;
        TextView email;
    }

    private void init() {
        Cursor ContactsCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        Log.e(TAG, ContactsContract.Contacts.CONTENT_URI.toString());
        Log.e(TAG, "data uri = " + Data.CONTENT_URI.toString());

        for (int i = 0; i < ContactsCursor.getColumnCount(); i++) {
            Log.e(TAG, "column name = " + ContactsCursor.getColumnName(i));
        }

        mAdapter = new CursorAdapter(ContactsActivity.this, ContactsCursor) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                View view = LayoutInflater.from(context).inflate(R.layout.list_item, null);

                ViewHolder holder = new ViewHolder();

                holder.icon = (ImageView) view.findViewById(R.id.friend_icon);
                holder.name = (TextView) view.findViewById(R.id.friend_name);
                holder.phone = (TextView) view.findViewById(R.id.friend_phone);
                holder.email = (TextView) view.findViewById(R.id.friend_email);

                view.setTag(holder);
                return view;
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                ViewHolder holder = (ViewHolder) view.getTag();

                /** get contacts id  */
                long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                /** get contacts name */
                String name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                holder.name.setText(name);

                /** check if this guy has phone */
                int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if (hasPhone > 0) {
                    /** get phone number */
                    Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                    if (phoneCursor.moveToFirst()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        holder.phone.setText(phoneNumber);
                    }
                }

                /** get email */
                Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
                if (emailCursor.moveToFirst()) {
                    String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    holder.email.setText(email);
                }

                /** get photo uri */
                Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
                Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
                Log.e(TAG ,"photoUri = " + photoUri.toString());
                holder.icon.setImageURI(photoUri);
            }

            @Override
            protected void onContentChanged() {
                super.onContentChanged();
            }
        };
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
        this.getListView().setAdapter(mAdapter);
        this.getListView().setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) {
                Toast.makeText(ContactsActivity.this, Build.VERSION.SDK, Toast.LENGTH_LONG).show();                
            }
        });

//        Cursor ContactsCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//        if (ContactsCursor.moveToFirst()) {
//            do {
//                /** get contacts id */
//                String contactId = ContactsCursor.getString(ContactsCursor.getColumnIndex(ContactsContract.Contacts._ID));
//                Log.e(TAG, "contact id = " + contactId);
//
//                String contact = ContactsCursor.getString(ContactsCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
//                Log.e(TAG, "contact = " + contact);
//
//                /** check if has phone */
//                int hasPhone = ContactsCursor.getInt(ContactsCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//
//                if ((hasPhone > 0)) {
//                    // get phone number
//                    Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
//                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
//                    while (phoneCursor.moveToNext()) {
//                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//                        Log.e(TAG, "phone number = " + phoneNumber);
//                    }
//                }
//
//                /** get emails */
//                Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
//                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
//
//                while (emailCursor.moveToNext()) {
//                    String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//                    Log.e(TAG, "email = " + email);
//                }
//
//                /** get photo uri of contacts */
//                Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
//                Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
//                Log.e(TAG, "uri = " + photoUri.toString());
//            } while (ContactsCursor.moveToNext());
//        }
    }
}